//视频
$(".btn-video")
                .parent("li:first")
                .click(
                                function() {
                                        currentClickTextId = -1;// 标记当前选中的不是textarea;
                                        currentClickImageId = -1;
                                        currentClickRadioId = -1;

                                        // 循环ID
                                        videoId++;
                                        currentClickVideoId = videoId;

                                        var videostr = "<div id='video-"
                                                        + videoId
                                                        + "' style='position: absolute;left:"+pptLeft+"px;top:"+pptTop+"px;'>"
                                                        + "<div id='video-title-"
                                                        + videoId
                                                        + "' class='video-tit'><span><img src='../images/ppt/icon01.png' class='v-middle'/>&nbsp;<strong>点击这里拖动</strong>&nbsp;</span>"
                                                        + "<img src='../images/ppt/error_tips.png' class='image-delete v-middle' onclick='divDelete(this);'/></div>"
                                                        + "<img id='video-img-"
                                                        + videoId
                                                        + "' src='../images/ppt/5.png' width='31px' height='31px' />"
                                                        + "</div>";
                                        $(".c-content").append(videostr);

                                        $(".video-layout").fadeIn("slow");

                                        videoListener(videoId);
                                        
                                        updateVideoLayout(videoId);
                                        
                                        //更新
                                        ppt.reSave();
                                        
                                        //上传地址清空
                                        $("#uploadVideo").val("");
                                });

/**
 * 视频监听事件
 */
function videoListener(videoId) {
        // 音频和配置数据都赋予拖拽功能
        $("#video-" + videoId).draggable({
                //containment : ".c-content"
        });

        $(".video-layout").draggable();
        // 标题 当按住标题才可以拖动
        $("#video-title-" + videoId).click().mousedown(function() {
            $("#video-" + videoId).draggable({
                //containment : ".c-content"
            });
                isMouseDown=true;
                currentClickVideoId = videoId;

                currentClickTextId = -1;
                currentClickImageId = -1;
                currentClickRadioId = -1;

                $("#video-" + videoId).fadeTo(10, 0.5);
                $("#video-" + videoId).css("z-index", "" + (++zindex));
                updateVideoLayout(videoId);
        });

        $("#video-title-" + videoId).click().mouseup(function() {
			    $("#video-"+videoId ).removeData("draggable").unbind(".draggable").removeClass("ui-draggable" + " ui-draggable-dragging"+ " ui-draggable-disabled");
                isMouseDown=false;
                $("#video-" + videoId).fadeTo(10, 1);
                updateVideoLayout(videoId);
        });

        $("#video-title-" + videoId).mousemove(function() {
                if(isMouseDown){
                        updateVideoLayout(videoId);
                }
        });
        
        $("#video-title-"+videoId).click().mouseout(function(){
        	$("#video-"+videoId).fadeTo(10,1);
        });

        $("#video-img-" + videoId).click().mousedown(function() {
                isMouseDown=true;
                currentClickVideoId = videoId;

                currentClickTextId = -1;
                currentClickImageId = -1;
                currentClickRadioId = -1;
                updateVideoLayout(videoId);
        });
        
        $("#video-img-" + videoId).click().mouseup(function() {
                isMouseDown=false;
                updateVideoLayout(videoId);
        });

        $("#video-img-" + videoId).click().mousemove(function() {
                if(isMouseDown){
                        updateVideoLayout(videoId);
                }
        });
}

/**
 * 更新属性框信息
 * 
 * @param videoId
 */
function updateVideoLayout(videoId) {
        var newpos = adjustPosition(".c-content", "#video-" + videoId,
                        "#video-content-" + videoId);

        var x = (isNaN(newpos.left) ? newpos.pleft : newpos.left);
        var y = (isNaN(newpos.top) ? newpos.ptop : newpos.top);;
        
        $("#video-x-coordinate").val(parseInt(x - newpos.pleft));
        $("#video-y-coordinate").val(parseInt(y - newpos.ptop));

        // 更改位置 x y
        $("#video-" + videoId).css("left", x);
        $("#video-" + videoId).css("top", y);

        
        //自动播放
        var check = $("#video-content-" + videoId).attr("autostart");
        if(check=="true"){
                $("#video-aotustart").attr("checked",true);
        }
        else{
                $("#video-aotustart").attr("checked",false);
        }
        
        //循环
        var check = $("#video-content-" + videoId).attr("loop");
        if(check=="loop"){
                $("#video-loop").attr("checked",true);
        }
        else{
                $("#video-loop").attr("checked",false);
        }
        
        
        var src = $("#video-content-" + videoId).attr("src");
        $("#videoUploadSrc").val(src);

}
/**
 * 确认更改数据
 */
$("#video-upload-submit").click(
                function() {

                        if (currentClickVideoId == -1) {
                                alert("请选 中一个 视频  框体");
                                return;
                        }
                        var w = "300", h = "300", autostart, loop;

                        try {
                                w = parseInt($("#video-width").val());
                                h = parseInt($("#video-height").val());
                        } catch (e) {
                                alert("请输入数字");
                                $("#video-width").val("300");
                                $("#video-height").val("300");
                        }

                        if ($("#video-aotustart").attr("checked") == "checked") {
                                autostart = true;
                        } else {
                                autostart = false;
                        }

                        if ($("#video-loop").attr("checked") == "checked") {
                                loop = true;
                        } else {
                                loop = false;
                        }

                        var src = $("#videoUploadSrc").val();

                        if (src != "") {
                                var srcc = src.toLowerCase().trim();
                                var suffix = srcc.substr(srcc.length - 3, srcc.length);

                                var html = "";
                                if (suffix == "flv") {
                                        html = "<embed src='../swf/vcastr22.swf?vcastr_file=" + src
                                        				+ "' id='video-content-"+ currentClickVideoId
                                                        + "' type='application/x-shockwave-flash' width='"
                                                        + w + "' height='" + h + "' quality='high' />";
                                }else if(suffix == "swf"){
                                	 html = "<embed src='" + src 
                                	 + "' id='video-content-"+ currentClickVideoId
                                	 + "' type='application/x-shockwave-flash' width='"
                                	 + w + "' height='" + h + "'quality='high' />";
                                }
                                
                                else {
                                        html = "<embed src='" + src + "' id='video-content-"
                                                        + currentClickVideoId
                                                        + "' type='video/x-ms-asf-plugin' width='" + w
                                                        + "' height='" + h + "' autostart='" + autostart
                                                        + "' loop='" + loop + "' />";
                                }
                                // 删除音频图标
                                $("#video-" + currentClickVideoId + ">img").remove();
                                $("#video-" + currentClickVideoId + ">embed").remove();
                                $("#video-" + currentClickVideoId).append(html);
                                $("#videoUploadSrc").val(src);
                        } else {
                                alert("请输入有效视频链接或者上传视频文件");
                        }

                });

/**
 * 文件大小
 */
$("#uploadVideo").change(function(){
	var f=this.files[0];
	var filesize = f.size||f.fileSize;
	if(filesize>30*1024*1024){
		alert("文件大小只能为30M以内");
		$("#uploadVideo").val("");
		return false;
	}
	return false;
});

function checkVideo() {
        var videofile = $("#uploadVideo").val();

        if (videofile == undefined || videofile == "" || videofile == null) {
                alert("请选择要上传的音频：avi,wmv,asf,mpg,flv.mp4");
                return false;
        }

        var index = videofile.lastIndexOf("\\");
        var name = videofile.substr(index + 1, videofile.length);
        index = name.lastIndexOf(".");
        var suffix = name.substr(index + 1, name.length).toLowerCase();
        
        if (suffix == "avi" || suffix == "wmv" || suffix == "asf"
                        || suffix == "mpg" || suffix == "flv" || suffix=="mp4") {
                return true;
        }

        alert("只能上传avi,wmv,asf,mpg,flash格式的音频");
        return false;
}

/**
 * 点击自动播放
*/

$("#video-aotustart").click(function(){
        var check = $(this).attr("checked");
        
        if(check=="checked" || check==true){
                $("#video-content-"+currentClickVideoId).attr("autostart",true);
        }
        else{
                $("#video-content-"+currentClickVideoId).attr("autostart",false);
        }
});

//循环
$("#video-loop").click(function(){
        var check = $(this).attr("checked");
        
        if(check=="checked" || check==true){
                $("#video-content-"+currentClickVideoId).attr("loop",true);
        }
        else{
                $("#video-content-"+currentClickVideoId).attr("loop",false);
        }
});



/**
 * 上下居中
 */
$("#video-up-down-center").click(function(){
    if(currentClickVideoId==-1){
        alert("请选 中一个音频框体");
        return;
    }
	
	//得到图片的左上角坐标
    var newpos = adjustPosition(".c-content", "#video-" + currentClickVideoId, "#video-content-" + currentClickVideoId);
    var h = newpos.h;
    
    //居中时左上角坐标，只要考虑top
    var newtop = parseInt((newpos.ptop+newpos.pendtop)/2-h/2)-9;
    $("#video-" + currentClickVideoId).css("top", newtop);
    $("#video-y-coordinate").val(parseInt(newtop - newpos.ptop));
    
});


/**
 * 左右居中
 */
$("#video-left-right-center").click(function(){
    if(currentClickVideoId==-1){
        alert("请选 中一个音频框体");
        return;
    }
	//得到图片的左上角坐标
    var newpos = adjustPosition(".c-content", "#video-" + currentClickVideoId, "#video-content-" + currentClickVideoId);
    var w = newpos.w;
    
    //居中时左上角坐标，只要考虑left
    var newleft = parseInt((newpos.pleft+newpos.pendleft)/2-w/2);
    $("#video-" + currentClickVideoId).css("left", newleft);
    $("#video-x-coordinate").val(parseInt(newleft - newpos.pleft));
});